package com.huike.clues.utils;

import java.util.Calendar;
import java.util.Date;

import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbRulePool;
import com.huike.clues.mapper.TbRulePoolMapper;
import com.huike.common.utils.spring.SpringUtils;

/**
 * 获取结束时间的工具类
 *
 * @author 86150
 */
public class HuiKeCrmDateUtils {
    public static Date getEndDateByRule(TbAssignRecord tbAssignRecord) {
        TbRulePoolMapper rulePoolMapper = SpringUtils.getBean(TbRulePoolMapper.class);
        TbRulePool rulePool = rulePoolMapper.getRule(tbAssignRecord.getType());
        if (rulePool == null) {
            return null;
        }
        return getDate(rulePool.getLimitTime().intValue(), rulePool.getLimitTimeType(), tbAssignRecord.getCreateTime());
    }

    public static Date getDate(int time, String type, Date now) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        if (TbRulePool.LimitTimeType.HOUR.getValue().equals(type)) {
            cal.add(Calendar.HOUR, time);
        } else if (TbRulePool.LimitTimeType.DAY.getValue().equals(type)) {
            cal.add(Calendar.DATE, time);
        } else if (TbRulePool.LimitTimeType.WEEK.getValue().equals(type)) {
            cal.add(Calendar.DAY_OF_WEEK, time);
        }
        return cal.getTime();
    }
}
